<?php

namespace app\wap\controller;

use app\common\controller\Mobile;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\Attachment;
use EasyWeChat\Factory;
use think\Config;
use think\Cookie;
use think\Hook;
use think\Session;
use think\Validate;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use fast\Random;
use compress\CompressImg;

/**
 * 员工签到
 *
 */
class Signed extends Mobile
{
    protected $noNeedLogin = ['autologin'];
    protected $noNeedRight = ['autologin'];
    protected $layout = 'danger';
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Person();
    }

    /**
     * 信息维护
     */
    public function index($deviceid = null,$type = null)
    {

        $deviceid = $deviceid ? $deviceid :$this->request->get('deviced', '');

//        //判断今天是否已经上传
//        $is_report = $this->model->where(['user_id'=>$this->auth->id,'deviceid'=>intval($deviceid),'is_xiwu'=>1])->whereTime('create_time','today')->order('id desc')->find();
//        if(!empty($is_report) && empty($type)){
//            $this->redirect('/wap/serviced/supple/deviceid/'.intval($deviceid).'/ids/'.$is_report['id']);
//            exit();
//        }
        if ($this->request->isPost())
        {
            $params = $this -> request -> post('row/a');
            $is_abnormal = 0;
            if($params['gufengji']=='不正常'){
                $is_abnormal = 1;
            }else if($params['tishengbeng']=='不正常'){
                $is_abnormal = 1;
            }else if($params['huiliubeng']=='不正常'){
                $is_abnormal = 1;
            }else if($params['tishengguan']=='不正常'){
                $is_abnormal = 1;
            }else if($params['huiliuguan']=='不正常'){
                $is_abnormal = 1;
            }else if($params['jiaobanqi']=='不正常'){
                $is_abnormal = 1;
            }else if($params['luocigufengji']!='正常'){
                $is_abnormal = 1;
            }else if($params['huixuangufengji']!='正常'){
                $is_abnormal = 1;
            }else if($params['kongqibengfengji']!='正常'){
                $is_abnormal = 1;
            }else if($params['changdi']=='不正常'){
                $is_abnormal = 1;
            }else if($params['shebeijian']=='不正常'){
                $is_abnormal = 1;
            }else if($params['chushuikou']!='正常'){
                $is_abnormal = 1;
            }else if($params['boli']=='不正常'){
                $is_abnormal = 1;
            }else if($params['xiwu']!='正常'){
                $is_abnormal = 1;
            }
            $data['user_id'] = $this->auth->id;
            $data['nickname'] = $this->auth->nickname;
            $data['mobile'] = $this->auth->mobile;
            $data['area'] = $this->auth->area;
            $data['report_json'] = json_encode($params);
            $data['deviceid'] = intval($deviceid);
            $data['is_abnormal'] = $is_abnormal;
            $data['is_xiwu'] = $params['xiwu']=='正常'?1:0;
            $ret = $this->model->save($data);
            $this->success('提交成功','/wap/serviced/index/deviceid/'.intval($deviceid).'/type/1');
        }

        $this -> assign('localday',date('Y年m月d日'));
        $this->assignconfig("deviceid", intval( $deviceid));
        return $this->view->fetch();
    }

    /**
     * 缓存自动登录 判断是否已经被关闭
     * [autologin description]
     * @return [type] [description]
     */
    public function autologin(){
        if ($this->request->isPost()) {
            $account = $this->request->post('account');
            $nickname = $this->request->post('password');
            $deviceid = $this->request->post('deviceid');
            $device = $this->device_model->get($deviceid);
            $info = Db::name('hx_user')->where(['username'=>$account,'nickname'=>$nickname,'status'=>1])->find();
            if($info['area']!=0 && $info['area']!=$device['area']){
                $this->error('设备所在区域与用户所在区域不一致');
            }
            if(!empty($info)){
                $this->success();
            }
            $admin = HxUser::get($this->auth->id);
            if (!$admin) {
                return true;
            }
            $admin->token = '';
            $admin->save();
            Session::delete("admin");
            Cookie::delete("keeplogin");
            $this->error();
        }
    }


}
